package com.aop.impl;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

//把这个类声明为一个切面，需要该该类放到IOC容器，再声明为一个切面。
@Aspect
@Component
public final class LoggingAspect {

	@Pointcut("execution(public int com.aop.impl.ComputerImpl.add(int, int))")
	public void ComputerImplAddMethod() {}
	
	@Before(value = "ComputerImplAddMethod()")
	public void beforemethod(JoinPoint joinPoint) {
		 String methodName = joinPoint.getSignature().getName();
		 List<Object> args = Arrays.asList(joinPoint.getArgs());
		 System.out.println("this method >" + methodName + " begins with >" + args);
	}

	// // 声明该方法是一个前置通知：在目标方法开始之前执行。
	// @Before("execution(public int com.aop.impl.ComputerImpl.add(int, int))")
	// public void beforemethod(JoinPoint joinPoint) {
	// String methodName = joinPoint.getSignature().getName();
	// List<Object> args = Arrays.asList(joinPoint.getArgs());
	// System.out.println("this method >" + methodName + " begins with >" + args);
	// }
	//
	// @After("execution(* *.jian(..))")
	// public void afterMethod(JoinPoint joinPoint) {
	// String methodName = joinPoint.getSignature().getName();
	// List<Object> args = Arrays.asList(joinPoint.getArgs());
	// System.out.println("this method >" + methodName + " begins with >" + args);
	// }
	//
	// @AfterReturning(value = "execution(* *.jian(..))", returning = "result")
	// public void afterReturningMethod(JoinPoint joinPoint, Object result) {
	// String methodName = joinPoint.getSignature().getName();
	// List<Object> args = Arrays.asList(joinPoint.getArgs());
	// System.out.println("this method >" + methodName + " begins with >" + args);
	// System.out.println("结果为:" + result);
	// }
	//
	// @AfterThrowing(value = "execution(* *.chu(..))", throwing = "ex")
	// public void AfterThrowingMethod(JoinPoint joinPoint, Exception ex) {
	// String methodName = joinPoint.getSignature().getName();
	// List<Object> args = Arrays.asList(joinPoint.getArgs());
	// System.out.println("this method >" + methodName + " begins with >" + args);
	// System.out.println("结果为:" + ex);
	// }

//	@Around(value = "execution(* *.*(..))")
	public Object AroundMethod(ProceedingJoinPoint pjp) {
		Object proceed = null;
		System.out.println("LoggingAspect.AroundMethod()");
		String methodName = pjp.getSignature().getName();
		Object[] args = pjp.getArgs();
		try {
			proceed = pjp.proceed();
		} catch (Throwable e) {
		}
		return proceed;
	}
}
